package com.xhs.service;

import com.xhs.dto.request.CommentRequest;
import com.xhs.entity.Comment;
import com.xhs.entity.Content;
import com.xhs.entity.User;
import com.xhs.repository.CommentRepository;
import com.xhs.repository.ContentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评论服务
 */
@Service
@Transactional
public class CommentService {
    
    @Autowired
    private CommentRepository commentRepository;
    
    @Autowired
    private ContentRepository contentRepository;
    
    /**
     * 添加评论
     */
    public Comment addComment(Long contentId, CommentRequest request, User user) {
        Content content = contentRepository.findById(contentId)
                .orElseThrow(() -> new RuntimeException("内容不存在"));
        
        Comment comment = new Comment();
        comment.setText(request.getText());
        comment.setUser(user);
        comment.setContent(content);
        
        // 如果是回复评论
        if (request.getParentId() != null) {
            Comment parent = commentRepository.findById(request.getParentId())
                    .orElseThrow(() -> new RuntimeException("父评论不存在"));
            comment.setParent(parent);
        }
        
        Comment savedComment = commentRepository.save(comment);
        
        // 更新内容的评论数
        content.incrementCommentCount();
        contentRepository.save(content);
        
        return savedComment;
    }
    
    /**
     * 删除评论
     */
    public void deleteComment(Long commentId, User user) {
        Comment comment = commentRepository.findById(commentId)
                .orElseThrow(() -> new RuntimeException("评论不存在"));
        
        // 检查权限
        if (!comment.getUser().getId().equals(user.getId()) && !user.getRole().equals(User.Role.ADMIN)) {
            throw new RuntimeException("无权限删除此评论");
        }
        
        commentRepository.delete(comment);
        
        // 更新内容的评论数
        Content content = comment.getContent();
        content.decrementCommentCount();
        contentRepository.save(content);
    }
    
    /**
     * 获取内容的评论列表
     */
    public Page<Comment> getContentComments(Long contentId, int page, int size) {
        Content content = contentRepository.findById(contentId)
                .orElseThrow(() -> new RuntimeException("内容不存在"));
        
        Pageable pageable = PageRequest.of(page, size);
        return commentRepository.findByContentAndParentIsNullOrderByCreatedAtDesc(content, pageable);
    }
    
    /**
     * 获取评论的回复列表
     */
    public Page<Comment> getCommentReplies(Long parentId, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return commentRepository.findRepliesByParentId(parentId, pageable);
    }
    
    /**
     * 获取内容的评论数
     */
    public long getCommentCount(Long contentId) {
        Content content = contentRepository.findById(contentId)
                .orElseThrow(() -> new RuntimeException("内容不存在"));
        
        return commentRepository.countByContent(content);
    }
} 